package com.pengweiwei.androidzygote.main.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.pengweiwei.androidzygote.R;
import com.pengweiwei.androidzygote.main.SpDao;
import com.pengweiwei.androidzygote.start.ImageGuideActivity;
import com.putong35.common.BuildConfig;
import com.putong35.common.account.AccountManager;
import com.putong35.common.account.ui.LoginActivity;
import com.putong35.common.app.FileManager;
import com.putong35.common.baseactfrag.activity.BaseActivity;
import com.putong35.common.utils.SLog;
import com.putong35.common.utils.io.FileUtil;
import com.putong35.common.utils.zip.ZipExtractor;
import com.tencent.bugly.Bugly;

public class MainActivity extends BaseActivity {
    public static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

     /*   TokenInfo info = new TokenInfo();
        info.setExpiresAt("");
        info.setAccessToken("AF318BE799982C97457092AEDA03CE4818724A13D80CF940E75D8AD2E24FB88ABD3165327F97AABB");
        info.setRefreshToken("AF318BE799982C97AECD790497EA6F4CD76F46F2ADECB9A9562CBD088F3E8D02E7A2A6D1E1419C0D");
        info.setServerTime("2016-07-21T14:43:41.080+0800");
        info.setMacKey("qjBDKRD6yJ");

        String url = "http://120.76.200.15:8888/uc/user";
        String authStr = Authorization.createAuthorization(info, url, "get");
        Log.e("vivi", "  auth string === " + authStr);*/

//        testGetAppAttrs();

        String path = FileManager.getWebCachePath(this) + "zygote_web_cache.zip";
        String toPath = FileManager.getWebCachePath(this) + "zygote_web_cache";
        FileUtil.copyAssetToSDCard(getAssets(), "www/zygote_web_cache.zip", path);

        ZipExtractor zipExtractor = new ZipExtractor(this,
                path, toPath,
                new ZipExtractor.OnExtractListener() {
                    @Override
                    public void onProgressUpdate(long currSize) {
                        SLog.e("onProgressUpdate currSize == " + currSize);
                    }

                    @Override
                    public void onComplete(long totalSize) {
                        SLog.e("onComplete totalSize == " + totalSize);
                    }

                    @Override
                    public void onFailure(int errorCode, long totalSize,
                                          long currSize) {
                        SLog.e("onFailure errorCode == " + errorCode + "totalSize" + totalSize);
                    }

                    @Override
                    public void onCaculateTotalSize(long totalSize) {
                        SLog.e("onCaculateTotalSize totalSize == " + totalSize);
                    }

                });
        zipExtractor.execute();
        statusTransucentAndLayoutFullscreen();

//        Intent splashIntent = new Intent(this, SplashActivity.class);
//        startActivityForResult(splashIntent, REQ_SPLASH);


        hideToolbar();

//        ActivityUtils.startActivity(this, CollectionListActivity.class);
    }

    @Override
    public View createDataView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        return layoutInflater.inflate(R.layout.activity_main, null);
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (AccountManager.getInstance().getAccount() == null) {
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            return;
        }

  /*      String url = "file:///android_asset/www/test_tel.html";
        MainBottomFragment menuFragment = (MainBottomFragment) getSupportFragmentManager().findFragmentById(R.id.fragBottomMenu);
        hideToolbar();*/

      /*  Intent intent = new Intent(this, MyMvpListActivity.class);
        startActivity(intent);*/
//      AccountManager.getInstance().clear();
    }


    public static final int REQ_SPLASH = 1;
    public static final int REQ_GUIDE = 2;
    public static final int REQ_LOGIN = 3;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQ_SPLASH:
                if (resultCode == RESULT_CANCELED) {
                    finish();
                } else if (resultCode == RESULT_OK) {
                    // goto guide page
                    if (!SpDao.isScanGuide(this)) {
                        Intent starter = new Intent(this, ImageGuideActivity.class);
                        startActivityForResult(starter, REQ_GUIDE);
                    } else {
                        gotoMain();
                    }
                }
                break;

            case REQ_GUIDE:
                if (resultCode == RESULT_CANCELED) {
                    finish();
                } else if (resultCode == RESULT_OK) {
                    SpDao.setScanGuide(this, true);
                    gotoMain();
                }
                break;

            case REQ_LOGIN:
                if (resultCode == RESULT_CANCELED) {
                    finish();
                } else if (resultCode == RESULT_OK) {
                    // 登录成功
                }
                break;


        }
    }

    private void gotoMain() {
        if (AccountManager.getInstance().getAccount() == null) {
            Intent intent = new Intent(this, LoginActivity.class);
            startActivityForResult(intent, REQ_LOGIN);
        } else {
            // bugly
            Bugly.init(getApplicationContext(), BuildConfig.BUGLY_APP_ID, false);
        }
    }


    /*
    void testGetAppAttrs() {
//        TypedArray typedArray1 = obtainStyledAttributes(R.styleable.IconTextArrowButton);
        TypedArray typedArray1 = obtainStyledAttributes(null,R.styleable.BadgeRadioButton,R.attr.badgeRadioButtonStyle,0);
        float drawHeight = typedArray1.getDimension(R.styleable.BadgeRadioButton_drawableHeight, -1.0f);
        Log.e("vivi", "   textSize  ==  " + drawHeight);
        typedArray1.recycle();

        TypedArray typedArray = obtainStyledAttributes(R.styleable.vidroid);
        float textSize1 = typedArray.getDimension(R.styleable.vidroid_textSize1, -1.0f);
        Log.e("vivi", "   textSize1  ==  " + textSize1);
        typedArray.recycle();
    }
*/

    private long lastBackPressedTime = 0;

    @Override
    public void onBackPressed() {
        long currPressedTime = System.currentTimeMillis();
        // 如果两秒内连续点击back press
        if (currPressedTime - lastBackPressedTime <= 2000) {
            super.onBackPressed();
        } else {
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            lastBackPressedTime = currPressedTime;
        }
    }
}
